package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.music.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.project.music.utils.Consts;

import javax.servlet.http.HttpSession;

/**
 * 管理员控制类
 */

@Api(tags = "管理员控制类")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("管理员登录")
    @PostMapping("admin/login/status")
    public JSONObject loginStatus(@RequestParam("name") @ApiParam("用户名") String name,
                              @RequestParam("password") @ApiParam("密码") String password,
                              HttpSession session) {
        JSONObject jsonObject = new JSONObject();

        boolean status = adminService.verify(name, password);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "登陆成功");
            session.setAttribute(Consts.NAME, name);
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "用户名或密码错误");
        }
        return jsonObject;
    }
}
